/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mudalov.rec.util.webui;

import com.mudalov.rec.dao.model.study.Question;
import com.mudalov.rec.dao.model.study.Test;
import com.mudalov.rec.dao.model.study.TestResult;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 *
 * @author MU
 */
public class TestHelper {

    private Test test;
    
    private int questioNumber;

    private int rightAnswers;
    
    private Question currentQuestion;

    private Question previousQuestion;
    
    private List<Question> questionsList;

    public TestHelper(Test test) {
        this.test = test;
        questioNumber = 0;
        rightAnswers = 0;
        questionsList = new ArrayList<Question>();
        Set<Question> questionsSet = test.getQuestions();
        for (Question question : questionsSet) {
            questionsList.add(question);
        }
    }

    public Question getNextQuestion() {
        previousQuestion = currentQuestion;
        if (questionsList != null
                && questioNumber < questionsList.size()) {
            currentQuestion = questionsList.get(questioNumber++);
            if (questioNumber == 1) {
                previousQuestion = currentQuestion;
            }
        } else {
            currentQuestion = null;
            questioNumber++;
        }
        return currentQuestion;
    }

    public Question getCurrentQuestion() {
        return currentQuestion;
    }

    public void registerRightAnswer() {
        rightAnswers++;
    }

    public Question getPreviousQuestion() {
        return previousQuestion;
    }

    public void setPreviousQuestion(Question previousQuestion) {
        this.previousQuestion = previousQuestion;
    }

    public TestResult getTestResult() {
        TestResult result = new TestResult();
        boolean passed =((double)rightAnswers)/((double)questionsList.size()) > 0.8;
        result.setDate(new Date());
        result.setPassed(passed);
        result.setPoints(rightAnswers);
        result.setTest(test);
        String testName = test != null ? test.getName() : "";
        result.setTestName(testName);
        return result;
    }

    public boolean isInProcess() {
         return (questionsList != null
                && questioNumber <= questionsList.size());
    }
}
